package com.jxc.jxcmanage.controller;

import com.jxc.jxcmanage.common.ResultBean;
import com.jxc.jxcmanage.service.UserInfoService;
import com.jxc.jxcmanage.code.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/userInfo")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户列表
     * @param param
     * @return
     */
    @PostMapping("/queryUserList")
    public ResultBean queryUserList(@RequestBody Map<String,Object> param) {
        String createdDateStart = (String)param.get("createdDateStart");
        String createdDateEnd = (String)param.get("createdDateEnd");
        if(StringUtils.hasLength(createdDateStart) && StringUtils.hasLength(createdDateEnd)){
            param.put("createdDateStart", createdDateStart + " 00:00:00");
            param.put("createdDateEnd", createdDateEnd + " 23:59:59");
        } else if(StringUtils.hasLength(createdDateStart) || StringUtils.hasLength(createdDateEnd)){
            return ResultBean.fail();
        }

        return ResultBean.success(userInfoService.queryUserList(param));
    }


    /**
     * 删除用户
     * @param param
     * @return
     */
    @PostMapping("/deleteUserInfoById")
    public ResultBean deleteUserInfoById(@RequestBody Map<String,Object> param){
        Integer id = (Integer) param.get("id");
        return ResultBean.success(userInfoService.deleteUserInfoById(id));
    }

    /**
     * 新增或者修改用户
     * @param userInfo
     * @return
     */
    @PostMapping("/addOrUpdateUserInfo")
    public ResultBean insertUserInfo(@RequestBody UserInfo userInfo) {
        return ResultBean.success(userInfoService.addOrUpdateUserInfo(userInfo));
    }

	@PostMapping(value = "/login")
	public ResultBean login(@RequestBody Map<String,String> param,
			HttpServletResponse response, HttpServletRequest request) throws Exception {
		UserInfo userInfo = new UserInfo();
        userInfo.setUserCode(param.get("username").toUpperCase());
		userInfo = userInfoService.findByCode(userInfo);
		if (ObjectUtils.isEmpty(userInfo)) {
			return ResultBean.fail("登陆失败，无此用户！", null);
		} else {
		    if(StringUtils.hasLength(param.get("password")) && param.get("password").equals(userInfo.getUserPassword())){
                request.getSession().setAttribute("loginuser", userInfo);
                return ResultBean.success("登陆成功！", null);
            } else {
                return ResultBean.fail("登陆失败，密码错误！", null);
            }
		}
	}
}
